package com.neuedu.amqp.consumer.listener;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

/**
 * @author 金山
 * 项目：sca-all-in-one
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/6/511:37
 */
@Component
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(String msg) {
        // JSON  {tel:11000,msg:'safdsafsafsa'}

        // 定义处理消息的逻辑
        System.out.println("Spring 消费者接收到的 消息：【" + msg + "】");
    }


    @RabbitListener(queues = "work.queue")
    public void listenWorkQueueMessage1(String msg) throws InterruptedException {
        // 定义处理消息的逻辑
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueueMessage2(String msg) throws InterruptedException {

        // 定义处理消息的逻辑
        System.err.println("消费者2接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(2000);
    }


    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueueMessage1(String msg) throws InterruptedException {

        // 定义处理消息的逻辑
        System.out.println("消费者1接收到fanout消息：【" + msg + "】" + LocalTime.now());
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueueMessage2(String msg) throws InterruptedException {

        // 定义处理消息的逻辑
        System.err.println("消费者2接收到fanout消息：【" + msg + "】" + LocalTime.now());
    }


    // Direct 交换机模式 接收第一个队列

    /**
     * routingKey ； blue ,red
     *
     * @param msg
     */
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1接收到direct.queue1消息：【" + msg + "】");
    }

    // Direct 交换机模式 接收第二个队列、

    /***
     * routingKey ； yellow ,red
     * @param msg
     */
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg) {
        System.err.println("消费者2接收到direct.queue2消息：【" + msg + "】");
    }


    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者1接收到Topic跟shanghai 有关的消息 消息：【" + msg + "】");
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg) {
        System.err.println("消费者2接收到Topic跟 news 有关的消息 消息：【" + msg + "】");
    }
}
